<template>
    <el-dialog :close-on-click-modal="false"  ref="dialogform" :title="addEditName + title" :visible.sync="dialogVisible" width="60%"
        :before-close="handleClose">
        <el-table :data="tableData" style="width: 100%" border>
            <el-table-column type="index" label="序号">
            </el-table-column>
            <el-table-column prop="item1" label="巡查点">
            </el-table-column>
            <el-table-column prop="item2" label="鱼塘名称">
            </el-table-column>
            <el-table-column prop="item3" label="批次名称">
            </el-table-column>
            <el-table-column prop="item4" label="巡查结果">
            </el-table-column>
            <el-table-column prop="item5" label="结果描述">
            </el-table-column>
            <el-table-column label="巡查图片">
                <template slot-scope="scope">
                    <span class="pointer primary" @click="viewImage(scope.row)">查看图片</span>
                </template>
            </el-table-column>
        </el-table>
        <span slot="footer" class="dialog-footer">
            <el-button @click="cancel">取 消</el-button>
            <el-button type="primary" @click="submitForm">确 定</el-button>
        </span>
    </el-dialog>
</template>
<script>
import MapPoint from '@/components/MapPoint';
export default {
    components: { MapPoint },
    props: {
        addEditFlag: {
            type: String,
            default: () => {
                return 'add'
            }
        },
        data: {
            type: Object,
            default: () => {
                return {}
            }
        },
        title: {
            type: String,
            default: () => {
                return '操作表单'
            }
        },
        dialogVisible: {
            type: Boolean,
            default: () => {
                return false
            }
        }
    },
    data () {
        return {
            addEditName: '添加',
            addEditApi: '',
            ruleForm: {},
            tableData: [
                {
                    item1: '巡查点1',
                    item2: '19#鱼塘',
                    item3: 'xxx批次',
                    item4: '巡查未发现异常',
                    item5: '巡查无异常',
                }
            ],
            rules: {
                item1: [
                    { required: true, message: '请输入巡查点名称', trigger: 'change' }
                ],
                item2: [
                    { required: true, message: '请选择巡查点', trigger: 'change' }
                ]
            }
        }
    },
    computed: {

    },
    watch: {
        dialogVisible (v) {
            v && this.initData()
        }
    },
    
    mounted () { },
    methods: {
        viewImage (data) {
            console.log(data)
        },
        initData () {
            if (this.addEditFlag == 'add') {
                this.resetForm()
                this.addEditApi = ''
                this.addEditName = '添加'
            } else if (this.addEditFlag == 'edit') {
                this.ruleForm = { ...this.data }
                this.addEditApi = ''
                this.addEditName = '编辑'
            }
        },
        drawPoint (item) {
            if (!item.point) {
                this.$message.warning('请先选择巡查点')
            } else {
                this.$refs.mapPoint.toggleMarkerMode(item.point, [])
            }
        },
        //提交表单
        submitForm () {
            this.$refs.ruleForm.validate((valid) => {
                if (valid) {
                    this.addEditApi.then(res => {
                        this.cancel()
                    }).catch(err => {

                    })
                } else {
                    return false;
                }
            })
        },
        //重置
        resetForm () {
            this.ruleForm = {};
            this.$refs.ruleForm && this.$refs.ruleForm.resetFields()
        },
        //取消关闭
        cancel () {
            this.resetForm()
            this.$emit("closeDialog")
        },
        handleClose (done) {
            this.$confirm('确认关闭？')
                .then(_ => { this.resetForm(); this.cancel () })
                .catch(_ => { });
        }
    }
};
</script>
<style lang="scss" scoped></style>
  